package tt.love.hw.taglib;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class LocaleTag extends TagSupport {

	private static final long serialVersionUID = -5504451508307711561L;

	public int doStartTag() {

		JspWriter out = pageContext.getOut();

		StringBuffer sb = new StringBuffer();

		try {
			String preUrl = (String) pageContext.getRequest().getAttribute(
					"preURL");
			if(preUrl==null){
				preUrl = ((HttpServletRequest)pageContext.getRequest()).getServletPath();
			}
			Locale currentLocale = (Locale) pageContext.getSession()
					.getAttribute("javax.servlet.jsp.jstl.fmt.locale");
			if (currentLocale == null) {
				currentLocale = new Locale("zh", "CN");
			}
			String currentLocaleLanguage = currentLocale.getLanguage();
			pageContext.getRequest().setAttribute("currentLocaleLanguage",
					currentLocaleLanguage);
			if (currentLocaleLanguage.equals("zh")) {
				sb = sb
						.append(
								"<span class=\"changelocale\" > <a class=\"nobg\" href=\"/changeLocale?code=en&currentpage=")
						.append(preUrl).append("\" >切换到英文</a></span>");
			} else {
				sb = sb
				.append(
						"<span class=\"changelocale\" > <a class=\"nobg\" href=\"/changeLocale?code=zh&currentpage=")
				.append(preUrl).append("\" >Swich to Chinese</a></span>");
			}
			out.print(sb.toString());
		} catch (Exception e) {
		}

		return (SKIP_BODY);
	}
}
